package com.first.controller;

import com.first.entity.UserEntity;
import com.first.service.UserService;
import com.first.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 现在看你的代码质量比上次提高了不少  加油++0.0
 */
@RestController
@RequestMapping("api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register/{userName}/{password}/{role}/{name}/{sex}/{address}")
    public R registerUser(@PathVariable("userName") String userName,
                          @PathVariable("password") String password,
                          @PathVariable("role") String role,
                          @PathVariable("name") String name,
                          @PathVariable("sex") String sex,
                          @PathVariable("address") String address)
    {
        UserEntity userEntity = userService.registerUser(userName,password,role,name,sex,address);
        if (userEntity!=null)
        {
            System.out.println("注册成功!");
            return R.ok().data("userName",userEntity.getName());
        }
        else
        {
            System.out.println("注册失败!");
            return R.error();
        }
    }

    @GetMapping("/login")
    public R loginUser(String userName,String password)
    {
        UserEntity userEntity = userService.loginUser(userName,password);
        if(userEntity!=null)
        {
            System.out.println("登陆成功!");
            return R.ok().data("user",userEntity);
        }
        else
        {
            return R.error();
        }
    }

    @DeleteMapping("/cancel")
    public R cancelUser(Integer id)
    {
        int i = userService.cancelUser(id);
        if(i>0)
        {
            System.out.println("注销成功!");
            return R.ok();
        }
        else
        {
            System.out.println("注销失败!");
            return R.error();
        }
    }

    @GetMapping("/usersList")
    public R getUsersList()
    {
        List<UserEntity> usersList = userService.getUsersList();
        return R.ok().data("list",usersList);
    }
}
